package pl.drypisy;

import java.io.File;
import java.util.Vector;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * Main class in aplication. Contains GUI and main() function.
 * @author Marcin
 */
public class AppGui extends javax.swing.JFrame {

    private AppControler ctrl;
    private File directory = null;
    //ew moża dodać
    private Vector<FileNameExtensionFilter> filters = new Vector<FileNameExtensionFilter>();
    
    /**
     * Constructor. Initiate gui and controler.
     */
    public AppGui() {
        initComponents();
        ctrl = new AppControler(panel,this);
        createFilters();
    }
    
    /**
     * Private function which create list of available image formats.
     */
    private void createFilters(){
        filters.add(new FileNameExtensionFilter("plik JPEG, *.jpeg","jpeg"));
        filters.add(new FileNameExtensionFilter("plik JPG, *.jpg","jpg"));
        filters.add(new FileNameExtensionFilter("plik GIF, *.gif","gif"));
        filters.add(new FileNameExtensionFilter("plik PNG, *.png","png"));
    }

    /**
     * Netbeans GUI form function.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrollPanel = new javax.swing.JScrollPane();
        panel = new pl.drypisy.ImagePanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        menuFile = new javax.swing.JMenu();
        mitemOpen = new javax.swing.JMenuItem();
        mitemSave = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        mitemScale = new javax.swing.JMenuItem();
        mitemRotation = new javax.swing.JMenuItem();
        mitemSkew = new javax.swing.JMenuItem();
        mitemLens = new javax.swing.JMenuItem();
        mitemPerspective = new javax.swing.JMenuItem();
        mitemBack = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(500, 350));

        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 755, Short.MAX_VALUE)
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 329, Short.MAX_VALUE)
        );

        scrollPanel.setViewportView(panel);

        menuFile.setText("Plik");

        mitemOpen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        mitemOpen.setText("Otwórz...");
        mitemOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mitemOpenActionPerformed(evt);
            }
        });
        menuFile.add(mitemOpen);

        mitemSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        mitemSave.setText("Zapisz...");
        mitemSave.setEnabled(false);
        mitemSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mitemSaveActionPerformed(evt);
            }
        });
        menuFile.add(mitemSave);

        jMenuBar1.add(menuFile);

        jMenu2.setText("Przekształcenia");

        mitemScale.setText("Skaluj obraz...");
        mitemScale.setEnabled(false);
        mitemScale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mitemScaleActionPerformed(evt);
            }
        });
        jMenu2.add(mitemScale);
        mitemScale.getAccessibleContext().setAccessibleDescription("");

        mitemRotation.setText("Rotacja obrazu");
        mitemRotation.setEnabled(false);
        mitemRotation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mitemRotationActionPerformed(evt);
            }
        });
        jMenu2.add(mitemRotation);

        mitemSkew.setText("Pochylenie...");
        mitemSkew.setEnabled(false);
        mitemSkew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mitemSkewActionPerformed(evt);
            }
        });
        jMenu2.add(mitemSkew);

        mitemLens.setText("Korekta soczewki...");
        mitemLens.setEnabled(false);
        mitemLens.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mitemLensActionPerformed(evt);
            }
        });
        jMenu2.add(mitemLens);

        mitemPerspective.setText("Perspektywa...");
        mitemPerspective.setEnabled(false);
        mitemPerspective.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mitemPerspectiveActionPerformed(evt);
            }
        });
        jMenu2.add(mitemPerspective);

        mitemBack.setText("Przywróc oryginalny obraz");
        mitemBack.setEnabled(false);
        mitemBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mitemBackActionPerformed(evt);
            }
        });
        jMenu2.add(mitemBack);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPanel)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollPanel)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     * Handler for open file menu item.
     * @param evt ActionEvent object
     */
    private void mitemOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mitemOpenActionPerformed
        JFileChooser fileChooser;
        if(directory != null){
            fileChooser = new JFileChooser(directory);
        }else{
            fileChooser = new JFileChooser();
        }
        fileChooser.setAcceptAllFileFilterUsed(false);
        for (FileNameExtensionFilter fltr : filters){
            fileChooser.addChoosableFileFilter(fltr);
        }
        int returnVal = fileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            directory = fileChooser.getCurrentDirectory() ;
            ctrl.loadImage(fileChooser.getSelectedFile());
            mitemSave.setEnabled(true);
            mitemBack.setEnabled(true);
            mitemScale.setEnabled(true);
            mitemSkew.setEnabled(true);
            mitemLens.setEnabled(true);
            mitemPerspective.setEnabled(true);
            mitemRotation.setEnabled(true);
            scrollPanel.updateUI();
        }
    }//GEN-LAST:event_mitemOpenActionPerformed

    /**
     * Handler for save file menu item.
     * @param evt ActionEvent object
     */
    private void mitemSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mitemSaveActionPerformed
        JFileChooser fileChooser;
        if(directory != null){
            fileChooser = new JFileChooser(directory);
        }else{
            fileChooser = new JFileChooser();
        }
        fileChooser.setAcceptAllFileFilterUsed(false);
        for (FileNameExtensionFilter fltr : filters){
            fileChooser.addChoosableFileFilter(fltr);
        }
        int returnVal = fileChooser.showSaveDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            directory = fileChooser.getCurrentDirectory();
            FileNameExtensionFilter form = (FileNameExtensionFilter)fileChooser.getFileFilter();
            ctrl.saveImage(fileChooser.getSelectedFile().getPath(), form.getExtensions()[0]);
            mitemSave.setEnabled(true);
        }
    }//GEN-LAST:event_mitemSaveActionPerformed
    /**
     * Handler for back-to-original-image menu item.
     * @param evt ActionEvent object
     */
    private void mitemBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mitemBackActionPerformed
        ctrl.backToOriginalImg();
    }//GEN-LAST:event_mitemBackActionPerformed
    /**
     * Handler for scale process menu item.
     * @param evt ActionEvent object
     */
    private void mitemScaleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mitemScaleActionPerformed
        ResizeDialog dial = new ResizeDialog(this, false, ctrl.getImageDimension(), ctrl);
        dial.setVisible(true);
    }//GEN-LAST:event_mitemScaleActionPerformed
    /**
     * Handler for skew process menu item.
     * @param evt ActionEvent object
     */
    private void mitemSkewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mitemSkewActionPerformed
        SkewDialog dial = new SkewDialog(this, false, ctrl.getImage(), ctrl);
        dial.setVisible(true);
    }//GEN-LAST:event_mitemSkewActionPerformed
    /**
     * Handler for lens correction process menu item.
     * @param evt ActionEvent object
     */
    private void mitemLensActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mitemLensActionPerformed
        LensCorrectionDialog dial = new LensCorrectionDialog(this, false, ctrl.getImage(), ctrl);
        dial.setVisible(true);
    }//GEN-LAST:event_mitemLensActionPerformed
    /**
     * Handler for perspective process menu item.
     * @param evt ActionEvent object
     */
    private void mitemPerspectiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mitemPerspectiveActionPerformed
        PerspectiveDialog dial = new PerspectiveDialog(this, false, ctrl.getImage(), ctrl);
        dial.setVisible(true);
    }//GEN-LAST:event_mitemPerspectiveActionPerformed
    /**
     * Handler for rotation process menu item.
     * @param evt ActionEvent object
     */
    private void mitemRotationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mitemRotationActionPerformed
        RotationDialog dial = new RotationDialog(this, false, ctrl.getImage(), ctrl);
        dial.setVisible(true);
    }//GEN-LAST:event_mitemRotationActionPerformed
    /**
     * Overrided repaint function. Add updating scrollPanel
     * @param evt ActionEvent object
     */
    @Override
    public void repaint(){
        super.repaint();
        scrollPanel.updateUI();
    }
    
    /**
     * Main function.
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AppGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AppGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AppGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AppGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AppGui().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu menuFile;
    private javax.swing.JMenuItem mitemBack;
    private javax.swing.JMenuItem mitemLens;
    private javax.swing.JMenuItem mitemOpen;
    private javax.swing.JMenuItem mitemPerspective;
    private javax.swing.JMenuItem mitemRotation;
    private javax.swing.JMenuItem mitemSave;
    private javax.swing.JMenuItem mitemScale;
    private javax.swing.JMenuItem mitemSkew;
    private pl.drypisy.ImagePanel panel;
    private javax.swing.JScrollPane scrollPanel;
    // End of variables declaration//GEN-END:variables
}
